package com.elong.android.specialhouse.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class BDMapUtils {

	/**
	 * 目的地图层
	 */
	public static final int OVERLAY_TYPE_DESTINATION = 1;
	/**
	 * 酒店图层
	 */
	public static final int OVERLAY_TYPE_HOTELITEM = 2;
	/**
	 * 酒店详情图层
	 */
	public static final int OVERLAY_TYPE_HOTELDETAILS = 3;
	/**
	 * 普通的地图图层
	 */
	public static final int OVERLAY_TYPE_NORMAL = 4;
	/**
	 * 图层类型对应的key名称
	 */
	public static final String OVERLAY_TYPE_KEY = "OverlayType";

	/**
	 * 目的地Ovrelay的ZIndex
	 */
	public static final int DESTINATION_ZINDEX = 10000;

	/**
	 * 存储百度地图Marker
	 */
	public static final List<BitmapDescriptor> BITMAP_LIST = new ArrayList<BitmapDescriptor>();

	/**
	 * 
	 * @Title: createHotelItemOverlay
	 * @Description: 创建地图酒店图层
	 * @param point
	 * @param view
	 * @param index
	 * @return OverlayOptions
	 * @throws
	 */
	public static final OverlayOptions createHotelItemOverlay(LatLng point, View view, int index) {
		// 构建Marker图标
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);
		if (BITMAP_LIST != null) {
			BITMAP_LIST.add(bitmap);
		}
		Bundle data = new Bundle();
		data.putInt(OVERLAY_TYPE_KEY, OVERLAY_TYPE_HOTELITEM);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point).icon(bitmap).zIndex(index).extraInfo(data);

		return option;
	}

	/**
	 * 
	 * @Title: createDestinationOverlay
	 * @Description: 创建目的地图层
	 * @param point
	 * @param view
	 * @return OverlayOptions
	 * @throws
	 */
	public static final OverlayOptions createHotelDetailsOverlay(LatLng point, View view) {
		// 构建Marker图标
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);
		if (BITMAP_LIST != null) {
			BITMAP_LIST.add(bitmap);
		}
		Bundle data = new Bundle();
		data.putInt(OVERLAY_TYPE_KEY, OVERLAY_TYPE_HOTELDETAILS);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point).icon(bitmap).extraInfo(data);

		return option;
	}

	/**
	 * 
	 * @Title: createDestinationOverlay
	 * @Description: 创建目的地图层
	 * @param point
	 * @param view
	 * @return OverlayOptions
	 * @throws
	 */
	public static final OverlayOptions createDestinationOverlay(LatLng point, View view) {
		// 构建Marker图标
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);
		if (BITMAP_LIST != null) {
			BITMAP_LIST.add(bitmap);
		}
		Bundle data = new Bundle();
		data.putInt(OVERLAY_TYPE_KEY, OVERLAY_TYPE_DESTINATION);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point).icon(bitmap).zIndex(DESTINATION_ZINDEX)
				.extraInfo(data);

		return option;
	}

	/**
	 * 
	 * @Title: createCustumizeOverlay
	 * @Description: 创建用户自定义图层
	 * @param point
	 * @param view
	 * @return OverlayOptions
	 * @throws
	 */
	public static final OverlayOptions createCustumizeOverlay(String title, LatLng point, View view) {
		// 构建Marker图标
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);
		if (BITMAP_LIST != null) {
			BITMAP_LIST.add(bitmap);
		}
		Bundle data = new Bundle();
		data.putInt(OVERLAY_TYPE_KEY, OVERLAY_TYPE_NORMAL);
		// 构建MarkerOption，用于在地图上添加Marker
		OverlayOptions option = new MarkerOptions().position(point).title(title).icon(bitmap).extraInfo(data);

		return option;
	}

	/**
	 * 
	 * @Title: recycleMarkers
	 * @Description: 回收所有地图页面的图片内存(注意:如果在详情页调用,会导致列表页图片被回收。仅限在列表页使用!详情页禁用!)
	 * @param
	 * @return void
	 * @throws
	 */
	public static final void recycleMarkers() {
		if (BITMAP_LIST != null) {
			for (BitmapDescriptor bitmap : BITMAP_LIST) {
				if (bitmap != null) {
					bitmap.recycle();
				}
			}

			BITMAP_LIST.clear();
		}
	}
	
	public static boolean isInstallBaiduApp(Context context) {
		List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
		for (PackageInfo info : packages) {
			if ("com.baidu.BaiduMap".equals(info.packageName)) {
				return true;
			}
		}
		return false;
	}
	
}
